#!/bin/sh

echo "###############################################################"
echo "### NUPP应用所涉及到的第三方软件检查脚本 "
echo "### 查看所依赖的第三方中间件是否正常安装及运行"
echo "###############################################################"

APPPATH=/home/gpp/app/

check_redis(){	
  pid=`ps ux | grep redis-server |  grep -v "grep" | awk '{print $2}'`
  if [ -n "$pid" ] ; then
    echo "redis		Server is running.        【OK】"
  else
    echo "redis		Server is not running.    【FAIL】"
  fi
}

check_minio(){	
  pid=`ps ux | grep minio | grep server | grep -v "grep" | awk '{print $2}'`
  if [ -n "$pid" ] ; then
    echo "minio		Server is running.        【OK】"
  else
    echo "minio		Server is not running.    【FAIL】"
  fi
}

check_elasticsearch(){	
  pid=`ps -ef | grep elasticsearch | grep java | grep -v "grep" | awk '{print $2}'`
  if [ -n "$pid" ] ; then
    echo "elasticsearch	Server is running.        【OK】"
  else
    echo "elasticsearch	Server is not running.    【FAIL】"
  fi
}

check_zookeeper(){	
  pid=`ps -ef | grep zookeeper | grep java | grep -v "grep" | awk '{print $2}'`
  if [ -n "$pid" ] ; then
    echo "zookeeper	Server is running.        【OK】"
  else
    echo "zookeeper	Server is not running.    【FAIL】"
  fi
}

check_rabbitmq(){	
  pid=`ps -ef | grep /lib/rabbitmq | grep erlang | grep -v "grep" | awk '{print $2}'`
  if [ -n "$pid" ] ; then
    echo "rabbitmq	Server is running.        【OK】"
  else
    echo "rabbitmq	Server is not running.    【FAIL】"
  fi
}

check_influxdb(){	
  pid=`ps -ef | grep influxd | grep -v "grep" | awk '{print $2}'`
  if [ -n "$pid" ] ; then
    echo "influxdb	Server is running.        【OK】"
  else
    echo "influxdb	Server is not running.    【FAIL】"
  fi
}

#### MAIN ####
if [ $# == 0 ]; then
  # 检查所有程序
  check_redis
  check_minio
  check_elasticsearch
  check_zookeeper
  check_rabbitmq
  check_influxdb
elif [ $# == 1 ]; then
  case $1 in 
    "redis")
      check_redis
      ;;
    "minio")
      check_minio
      ;;	  
    "elasticsearch")
      check_elasticsearch
      ;;	  
    "zookeeper")
      check_zookeeper
      ;;
    "influxdb")
      check_influxdb
      ;;
    *)
      echo "invalid parameter [$1]..."
      ;;	  
  esac
fi



